package com.sinosoft.controller.system;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sinosoft.common.Const;
import com.sinosoft.entity.DefUser;
import com.sinosoft.service.system.ICompanyService;
import com.sinosoft.service.system.IUserService;

@Controller
@RequestMapping("/system")
public class UserController {

	@Autowired
	private IUserService userService;
	@Autowired
	private ICompanyService companyService;
	
	@RequestMapping("/user")
	public ModelAndView user(ModelAndView modelAndView) {
		modelAndView.setViewName("system/user");
		return modelAndView;
	}
	
	@ResponseBody
	@RequestMapping("/initUserTable")
	public Map<String, Object> initUserTable(DefUser user, int page) {
		user.setStart((page - 1) * user.getRows());
		return userService.selectUserByCondition(user);
	}
	
	@RequestMapping("/userAddPage")
	public ModelAndView userAddPage(ModelAndView modelAndView) {
		modelAndView.setViewName("system/userAdd");
		return modelAndView;
	}
	
	@ResponseBody
	@RequestMapping("/addUser")
	public Map<String, Object> addUser(DefUser user, HttpServletRequest request) {
		DefUser us = new DefUser();
		us.setUserCode(user.getUserCode());
		Map<String, Object> usMap = userService.selectUserByCondition(us);
		Object usObj = usMap.get("total");
		Map<String, Object> map = new HashMap<String, Object>();
		if (null != usObj && (Integer) usObj > 0) {
			map.put("flag", false);
			map.put("msg", "添加失败！用户编码已经存在！");
			return map;
		}
		
		DefUser u = (DefUser) request.getSession().getAttribute(Const.SESSION_USER_KEY);
		user.setCreateUserId(u.getUserId());
		user.setModifyUserId(u.getUserId());
		try {
			map = userService.addUser(user);
		} catch (Exception e) {
			map.put("flag", false);
			map.put("msg", e.getMessage());
		}
		return map;
	}
	
	@RequestMapping("/userModifyPage")
	public ModelAndView userModifyPage(ModelAndView modelAndView) {
		modelAndView.setViewName("system/userModify");
		return modelAndView;
	}

	@ResponseBody
	@RequestMapping("/modifyUser")
	public Map<String, Object> modifyUser(DefUser user, HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		DefUser u = (DefUser) request.getSession().getAttribute(Const.SESSION_USER_KEY);
		user.setModifyUserId(u.getUserId());
		try {
			map = userService.modifyUser(user);
		} catch (Exception e) {
			map.put("flag", false);
			map.put("msg", e.getMessage());
		}
		return map;
	}
	
	@ResponseBody
	@RequestMapping("/deleteUser")
	public Map<String, Object> deleteUser(Integer userId) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			map = userService.deleteUser(userId);
		} catch(Exception e) {
			map.put("flag", false);
			map.put("msg", e.getMessage());
		}
		return map;
	}
}
